<?php
/************************
* Simple News Engine 0.2.3
* author: Ragnaar <ragnaar@km.ru>
* home: http://code.google.com/p/sne-news
* date: 08.06.2011
*********************/
define('_VALID_SNE', '1');
define('SNE_PATH', dirname(__FILE__));

// keep our session
session_start();

require_once SNE_PATH."/config.php";
require_once SNE_PATH."/mrthumb.class.php";

// The image you are resizing. Can be a local path as well.
$image = $_FILES['myfile']['tmp_name'];
$type = $_FILES['myfile']['type'];

$filetypes = array(
    'image/pjpeg' => 'jpg',
	'image/jpeg' => 'jpg',
	'image/gif' => 'gif',
	'image/bmp' => 'bmp',
	'image/png' => 'png',
	'image/x-png' => 'png'
);
$extension = $filetypes[$type];

// Start Mr. Thumb v1.0
$mrthumb = new MrThumb();

// Render the image
$mrthumb->render( $image, $type );

// Resize the image proportionately
// $mrthumb->constrain( $width, $height );
$mrthumb->proportion( $width, $height );

// Create a unique id or name for the image
$file_id = md5($image + rand()*100000);
$new_name = $file_id.".".$extension;
$temp_name = $image;

// Assign upload directory, and target path for image
$upload_dir_img = "../image/";
$upload_dir_thb = "../thumb/";

$target_path = $upload_dir_img.$new_name;

// Move uploaded image to uploaddir location
if(!@move_uploaded_file($temp_name, $target_path)){
	$err = "Couldn't Copy File to Filesystem";
}

// Finally, output the image to the browser!
// $mrthumb->output( $quality );

// Optionally we can save the image to a destination
$mrthumb->saveto( $upload_dir_thb, $file_id, $quality );

// Clean up after you are done! ;)
$mrthumb->clear_cache();

// If no error, then pass good result and file id to Javascript for generating the thumbnail.
// Pass our $err varialbe along

if(isset($err)){
    $result = "'.$err.','".$new_name."'";
} else {
	$result = "'1','".$new_name."'";
}

sleep(1); // in case of really fast upload, sleep script to show loading animation for a bit longer

// Javascript calls the "stopUpload" function with its paramaters
?>
<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result; ?>);</script>
